﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TestSystem.BLL.TestSystem;


public partial class Admin_ManageSubjects : System.Web.UI.Page
{
    private List<Subject> subjects;
    public List<Subject> Subjects
    {
        get
        {
            if (subjects == null)
                subjects = (List<Subject>)subjectsObjSource.Select();
            return subjects;
        }
        set
        {
            subjects = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            
        }
    }
    
    protected void lvwSubjects_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        e.Cancel = true;
        int subjectId = Convert.ToInt32(e.Keys["SubjectID"]);
        var currSubject = Subjects.Find(subj => subj.SubjectID == subjectId);
        currSubject.Description = e.NewValues["Description"] == null ? null : e.NewValues["Description"].ToString();
        currSubject.SubjectName = e.NewValues["SubjectName"].ToString();
        currSubject.Update();
        lvwSubjects.EditIndex = -1;
        lvwSubjects.DataBind();
        Subjects = null;
    }

    protected void lvwSubjects_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if(e.CommandName == "Restore")
        {
            int subjectId = Convert.ToInt32(e.CommandArgument);
            var currSubject = Subjects.Find(subj => subj.SubjectID == subjectId);
            currSubject.Deleted = false;
            currSubject.Update();
            lvwSubjects.DataBind();
            Subjects = null;
        }
        if(e.CommandName == "AddQuiz")
        {
            int subjectId = Convert.ToInt32(e.CommandArgument);
            Response.Redirect("ManageQuizes.aspx?subjectID=" + subjectId + "#insert");
        }
        if (e.CommandName == "ShowAllQuizes")
        {
            int subjectId = Convert.ToInt32(e.CommandArgument);
            Response.Redirect("ManageQuizes.aspx?subjectID=" + subjectId);
        }
    }

    protected void lvwSubjects_ItemInserted(object sender, ListViewInsertedEventArgs e)
    {
        Subjects = null;
    }

    protected void lvwSubjects_ItemDeleted(object sender, ListViewDeletedEventArgs e)
    {
        Subjects = null;
    }
}    
